﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SlamCms.Areas._admin.Models;
using SlamCms.Common;
using SlamCms.Configuration;
using SlamCms.Data;
using SlamCms.Web;

namespace SlamCms.Areas._admin.Controllers
{
	public class BarController : AdminBaseController
	{
		private readonly ICookieManager _cookieManager;
		private readonly SlamCmsContext _slamCmsContext;

		public BarController(SlamCmsContext slamCmsContext)
		{
			_slamCmsContext = slamCmsContext;
			_cookieManager = slamCmsContext.HttpContext.GetCookieManager();
		}

		[ChildActionOnly]
		public ActionResult Index()
		{
			if (!_slamCmsContext.AdministrationBar.Visible)
				return new EmptyResult();

			return PartialView();
		}

		[ChildActionOnly]
		public ActionResult IndexHtml()
		{
			var user = _slamCmsContext.User;

			var model = new BarIndexViewModel();
			model.ShowLogo = !ConfigurationHelpers.GetEnvironmentSetting(Constants.Settings.AdminBarDisableLogo, false);
			model.UserName = user.Name;
			model.AdminUrl = ConfigurationManager.Environment.DefaultApplication.AdminUrl;
			model.PreviewMode = _slamCmsContext.AdministrationBar.PreviewMode;

			var contentItem = _slamCmsContext.AdministrationBar.ContentItem;
			if (contentItem != null)
			{
				model.ContentItem = new ContentItemInfo();
				model.ContentItem.ContentItemId = contentItem.ContentItemId;
				model.ContentItem.IsDraft = contentItem.IsDraft();
				model.ContentItem.CanEdit = _slamCmsContext.AdministrationBar.CustomCanEdit ?? user.CanEdit(contentItem);
				model.ContentItem.EditUrl = _slamCmsContext.AdministrationBar.CustomEditUrl ?? Url.ContentItemEdit(contentItem.ContentItemId);
				model.ContentItem.MenuItems = _slamCmsContext.AdministrationBar.MenuItems;

                if (IsContentExpirationEnabled() && IsContentAboutToExpire(contentItem) && _slamCmsContext.User.IsContentExpirationAdmin())
				{
					model.ContentItem.ShowExpirationBox = true;
					model.ContentItem.Expired = contentItem.ExpirationDate.Value < DateTime.Now.Date;
					model.ContentItem.ExpirationDate = contentItem.ExpirationDate.Value;

					var expirationMessage = "Content Expires On:";
					if (model.ContentItem.Expired)
						expirationMessage = "Content Expired On:";

					model.ContentItem.ExpirationMessage = expirationMessage.F(contentItem.ExpirationDate.Value.ToShortDateString());

					model.ContentItem.CanVerify = _slamCmsContext.User.CanApprove(contentItem);
					model.ContentItem.CanRetire = _slamCmsContext.User.CanDelete(contentItem);
				}
			}

			return this.JavascriptEmbeddedHtml("IndexHtml", model);
		}

		[HttpGet]
		public ActionResult Open(bool? goToAdmin)
		{
			_cookieManager.Set(Constants.Cookies.AdminBar);
			if (goToAdmin ?? false)
				return Redirect(ConfigurationManager.Environment.DefaultApplication.AdminUrl);
			else
				return this.RedirectToReferer();
		}

		[HttpGet]
		[OutputCache(Duration = 0)]
		public ActionResult TogglePreviewMode()
		{
			_cookieManager.Toggle(Constants.Cookies.PreviewMode);

			return this.RedirectToReferer();
		}

		[HttpGet]
		[OutputCache(Duration = 0)]
		public ActionResult Close()
		{
			_cookieManager.Remove(Constants.Cookies.PreviewMode);
			_cookieManager.Set(Constants.Cookies.AdminBar, "0");

			return this.RedirectToReferer();
		}

        protected bool IsContentExpirationEnabled()
        {
            return ConfigurationHelpers.GetEnvironmentSetting(Constants.Settings.ContentExpirationEnabled, true);
        }

	    protected bool IsContentAboutToExpire(ContentItem contentItem)
	    {
	        var aboutToExpireDays = ConfigurationManager.Environment.Settings.Get(Constants.Settings.ContentExpirationAboutToExpireDays, 30);
	        return contentItem.ExpirationDate.HasValue && DateTime.Now.Date > contentItem.ExpirationDate.Value.AddDays(-aboutToExpireDays);
	    }
    }
}